#ifndef RCBUILDTASK_H
#define RCBUILDTASK_H

#include <QObject>
#include <QStringList>
#include <QTimer>

namespace WccNameSpace{
    namespace Internal {
    // from WccApi
        class WccBuild;
    }
}
namespace WccApi = WccNameSpace::Internal;

class GeneratorOutputView;
class RcSession;

/**
  * This class represents a remote build task.
  *
  * Usage: create an instance, include files for compilation,
  *
  */
class RcBuildTask : public QObject
{
    Q_OBJECT

private:
    //
    // private members
    //
    GeneratorOutputView         & m_generatorOutput;
    RcSession                   & m_session;

    QString                       m_projectName;
    QString                       m_projectDir;
    QStringList                   m_projectFiles;

    QString                       m_downloadDir;
    QString                       m_wccParams;
    QString                       m_savedPath;

    WccApi::WccBuild            * m_build;

    QTimer                        m_timeout;

public:
    /**
      * @param ui to put messages and error messages to.
      *
      * @param dir the path to the directory contaning the
      *        sources to include in the remote build.
      */
    RcBuildTask(GeneratorOutputView       & generatorOutput,
                RcSession                 & session,
                const QString             & projectName,
                const QString             & projectDir,
                QObject                   * parent = NULL);

    /**
      * Starts the process of building. Build steps are:
      *
      * (a) zip the source directory
      * (b) uploads it to the remote builder service
      * (c) downloads it when ready t dstDir
      *
      * @param wccParams is the string fragment to include
      * in the URL request, identifying the platform and qt version.
      * Example: "pSet[os]=s60_3_1&pSet[qt]=4_6_2".
      *
      * @param downloadDir destination dir to download the zip to.
      */
    void startBuilding(const QString & wccParams,
                       const QString & downloadDir);


signals:
    /**
      * For status updating: emitted when starting to zip the project
      * directory for sending to RC.
      */
    void zipping();

    /**
      * For status updating: emitted when starting to upload the zipped
      * project package.
      */
    void uploadingAndWaiting();

    /**
      * For status updating: emitted when starting to wait for the
      * uploaded package to be compiled.
      */
    void downloading();

    /**
      * Emitted when the build has been completed, either successfully
      * or due to an error.
      */
    void buildCompleted(bool    success,
                        QString downloadedFile);


private slots:
    void zipProcessSucceedSlot(const QString &buildUrl, const QString &zipFileName);
    void zipProcessFailedSlot(const QString &eMsg);
    void sendZipOkSlot();
    void uploadedSlot(qint64 sent,
                      qint64 total);
    void operationTimedOutSlot();
    void downloadFinishedSlot();


private:
    /**
      * Traverses m_projectDir recursively and puts all files found
      * to m_projectFiles string list.
      */
    void traverseProjectDir();

    /**
      * Parses the log of compiling obtained from the remote build service.
      */
    void parseBuildLog(const QString & fullLog);

};


#endif // RCBUILDTASK_H
